<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\VoiceCallOp;
use App\Http\Controllers\Controller;
use App\Model\CallBase;
use App\Model\Message;
use App\Model\VideoCall;
use App\Model\VoiceCall;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class ChattingController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('正在通话中列表');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new VideoCall());
        $status = [CallBase::STATUS_ING, CallBase::STATUS_NO_PICK];
        // $videoResult = VideoCall::whereIn('status', $status);
        $grid->model()->whereIn('status', $status)->orderBy('id', 'desc');
        // $grid->id('话单ID');
        $grid->call_type('话单类型')->display(function ($data) {
            return $data == VideoCall::CALL_TYPE ? '视频' : '语音';
        });
        $grid->status('话单状态')->display(function ($data) {
            return CallBase::STATUS[$data];
        });
        $grid->from('话单来源')->display(function ($data) {
            return CallBase::FROM_TYPE[$data];
        });
        $grid->fromUser()->user_profile_id('拨打方ID');
        $grid->fromUser()->nick_name('拨打方昵称');
        $grid->toUser()->user_profile_id('接听方ID');
        $grid->toUser()->nick_name('接听方昵称');
        $grid->start_time('发起时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', $time);
        });
        $grid->answer_time('接听时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', $time);
        });
        $grid->valid_call_time('可通话时长')->display(function ($time) {
            $this->validTime = VideoCall::totalValidTime($this->user_id, $this->from, $this->coin_setting);
            return  $this->validTime . "分钟";
        });
        $grid->past_call_time('当前通话时长')->display(function ($time) {
            return (!$this->answer_time ? 0 : ceil((time() - $this->answer_time) / 60)) . "分钟";
        });

        //禁用导出
        $grid->disableExport();
        //禁用行选择
        $grid->disableRowSelector();
        // $grid->disableActions();
        $grid->disableCreateButton();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('fromUser.user_profile_id', '拨打方ID');
                $filter->equal('from', '话单来源')->select(VideoCall::FROM_TYPE);
                $filter->between('created_at', '发起时间')->datetime();
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('toUser.user_profile_id', '接听方ID');
                $filter->equal('call_type', '话单类型')->select([0 => '语音', 1 => '视频']);
            });
        });
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
            $actions->append(new VoiceCallOp(
                $actions->getKey(),
                $actions->row,
                'voice_call/op'
            ));
        });
        return $grid;
    }

    /**
     * 话单操作
     */
    function call_op()
    {
        $id = request('id');
        $callType = request('extra');
        $callCls = $callType == VoiceCall::CALL_TYPE ? VoiceCall::class : VideoCall::class;
        $record = $callCls::where('id', $id)->first();
        if (!$record) return ['ret' => -1, 'message' => "找不到该话单记录！"];
        //修改状态
        $ts = time();
        $info = [
            'status' => $callCls::STATUS_ADMIN_HANGUP,
            'end_time' => $ts,
        ];
        if ($record->answer_time > 0) $info['call_time'] = $ts - $record->answer_time;
        $record->update($info);
        //暂停通话
        Message::sendSlient($record->user_id, 'HANGUP_CALL', ['tip' => '本次通话已结束～']);
        Message::sendSlient($record->chat_user_id, 'HANGUP_CALL', ['tip' => '本次通话已结束～']);
        //由声网回调自己结算
        return ['ret' => 0, 'message' => "成功关闭话单！ID：{$id}"];
    }
}
